<%--
Document : connected
Created on : 16 déc. 2013, 09:40:43
Author : root
--%>

<%@page import="presenter.Presenter"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Vector"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<% HttpSession thisSession = (HttpSession) request.getAttribute("thisSession");
    String name = "";
    String namAffiche = "";
    String conv = "";
    String demande = "";
    String filesent = "";
    if ((String) request.getAttribute("filesent") != null) {
        filesent = (String) request.getAttribute("filesent");
    }
    ArrayList<String> contacts = (ArrayList<String>) thisSession.getAttribute("contacts");
    Map<String, String> onglets = (HashMap<String, String>) thisSession.getAttribute("Onglets");
    ArrayList<String> enDiscussion = new ArrayList<String>();
    ArrayList<String> newMessage = (ArrayList<String>) thisSession.getAttribute("newMessage");
    ArrayList<String> demandeContact = (ArrayList<String>) thisSession.getAttribute("demandeContact");
    ArrayList<String> demandeAccepte = (ArrayList<String>) thisSession.getAttribute("demandeAccepte");
    String messageToSend = (String) thisSession.getAttribute("messageToSend");
    String wasRefreshed = (String) (thisSession.getAttribute("refreshed"));
    int ancientPageYScroll = Integer.parseInt((String) thisSession.getAttribute("scrollY"));
    int ancientCaretPos = Integer.parseInt((String) thisSession.getAttribute("caretPos"));
    String ongletactif = (String) thisSession.getAttribute("ongletActif");
    String msgFile = "";
    int timeToWait = 1;
    Map<String, String> waitingFile = (HashMap<String, String>) request.getAttribute("waitingFile");
    if (waitingFile.size() > 0) {
        msgFile = msgFile + "(" + waitingFile.size() + ")";

    }
    if (demandeContact.size() != 0) {
        boolean accept = true;
        int notif = demandeContact.size();
        if (notif != 0) {
            demande = " (" + notif + ")";
        }
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/connected.css"/>
        <title>MIS-System</title>
        <script type="text/javascript">
            //<!--
            function change_onglet(name)
            {
                document.getElementById('onglet_' + anc_onglet).className = 'onglet_0 onglet';
                document.getElementById('onglet_' + name).className = 'onglet_1 onglet';
                document.getElementById('onglet_' + name).innerHTML = name + " <input TYPE=\"button\" VALUE=\"x\" onclick=\"window.location.href ='CloseDiscussion?closeOnglet=" + name + "'\"/>";
            <% int j = 0;
                boolean existe = false;
                while (j < newMessage.size() && !existe) {
                    if (newMessage.get(j).equals(name)) {
                        existe = true;
                        namAffiche = name + "*";
                    }
                    j++;
                }
            %>
                document.getElementById('contenu_onglet_' + anc_onglet).style.display = 'none';
                document.getElementById('contenu_onglet_' + name).style.display = 'block';
                anc_onglet = name;
                document.getElementById("destinataire").value = name;
                document.getElementById("destinatairefile").value = name;
                thisSession.setAttribute("ongletActif", name);
            }
            //-->
        </script>
        <script type="text/javascript">time = 0;</script>
        <!--<script type="text/javascript">
        function refresh() {
        var message = document.getElementById("messageToSendArea").value;
        javascript:document.location.href = 'RedirectConnected';
        document.getElementById("messageToSendArea").value = message;
        }
        </script>-->
        <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
        <!--
        <script type="text/javascript" src="rescuefieldvalues.js"></script>
        <script type="text/javascript" src="script/refresh.js"></script>
        -->
        <script type="text/javascript">
            function doGetCaretPosition(ctrl) {
                var CaretPos = 0;
                // IE Support
                if (document.selection) {
                    ctrl.focus();
                    var Sel = document.selection.createRange();
                    Sel.moveStart('character', -ctrl.value.length);
                    CaretPos = Sel.text.length;
                }
                // Firefox support
                else if (ctrl.selectionStart || ctrl.selectionStart == '0')
                    CaretPos = ctrl.selectionStart;
                return (CaretPos);
            }
            function setCaretPosition(ctrl, pos) {
                if (ctrl.setSelectionRange) {
                    ctrl.focus();
                    ctrl.setSelectionRange(pos, pos);
                }
                else if (ctrl.createTextRange) {
                    var range = ctrl.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character', pos);
                    range.moveStart('character', pos);
                    range.select();
                }
            }
        </script>
    </head>
    <body>
        <header>
            <a href="RedirectConnected"><img src="Pictures/chat.png" class="chat" style="float: outside;"><img src="Pictures/logo.png" class="logo" style="float: outside;"></a>
            <div class="zone">
                <div class="zone-right"></div>
                <div class="zone-left"></div>
                <div class="zone-bg">
                    <ul>
                        <li class="current"><a onClick="refresher()"><span>Home</span></a></li>
                        <li><a href="NewContact.html"><span>Contact <%= demande%></span></a></li>
                        <li><a href="profile.html"><span>Profile</span></a></li>
                        <li><a href="File.html"><span>File <%= msgFile%></span></a></li>
                        <li><a href="Administrator.html"><span>Administration</span></a></li>
                        <li><a href="logout.html"><span>Logout</span></a></li>
                    </ul>
                </div>
            </div>
        </header>

        <table>
            <tr>
                <td>
                    <% String imStat = Presenter.getStatusIconAddrBig((String) thisSession.getAttribute("login"));%>
                    <% String status = imStat.substring(0, imStat.indexOf('2'));%>
                    <h2><img title="You are <%=status%>" src="Pictures/Icons/<%=imStat%>" height = "40" width="40" /> Hello <%= thisSession.getAttribute("login")%> </h2>
                </td>

                <td id="changestatus"   style="vertical-align:bottom;">
                    <form method="post" action="ChangeStatus">
                        <select name="ListeStatus">
                            <option value="Connected">Connected</option>
                            <option value="Away">Away</option>
                            <option value="Busy">Busy</option>
                        </select>
                        <input type="submit" value="Ok">
                    </form><br>
                </td>
            </tr>
        </table>

        <div id='contactlist'>
            <ul>
                <% for (String contact0 : contacts) {
                        String imAddr = Presenter.getStatusIconAddr(contact0);
                        String contactStatus = imAddr.substring(0, imAddr.indexOf('.'));%>

                <li class='has-sub'><a><img title="<%=contact0%> is <%=contactStatus%>" src="Pictures/Icons/<%=imAddr%>"/><span> <%= contact0%></span></a>
                    <ul>
                        <li><% out.println("<a href=\"BeginDiscussion?ListeContact=" + contact0 + "\"><span>Begin a discussion</span></a>");%></li>
                        <li class='has-sub'><a><span>Send file</span></a>
                            <ul>
                                <li><a>
                                        <form method="POST" action="FileUpload" enctype="multipart/form-data">
                                            <input type="hidden" name="menu" value="true"/>
                                            <input type="hidden" name="dest" value="<%=contact0%>"/>
                                            <input type="file" name="file" id="file" onclick="changeTime()"/><br>
                                            <input type="submit" value="Upload" name="upload" id="upload">
                                        </form>
                                    </a></li>
                            </ul>
                        </li>
                        <li class='last'><% out.println("<a href=\"DeleteContact?contacts=" + contact0 + "\"><span>delete</span></a>"); %></li>
                    </ul>
                </li>
                <% } %>
            </ul>
            <a href="AddContact.html" class="buttonaddcontact">Add contact</a>
        </div>

        <div id="Conversation">
            <div class="onglets">
                <% for (Entry<String, String> entry : onglets.entrySet()) {
                        name = entry.getKey();
                        if (!name.equals("Home")) {
                            namAffiche = name + " <input TYPE=\"button\" VALUE=\"x\" onclick=\"window.location.href ='CloseDiscussion?closeOnglet=" + name + "'\"/>";
                        }
                        else {
                            namAffiche = name;
                        }
                        int i = 0;
                        boolean trouve = false;
                        while (i < newMessage.size() && !trouve) {
                            if (newMessage.get(i).equals(name)) {
                                trouve = true;
                                namAffiche = name + "*";
                            }
                            i++;
                        }
                        conv = entry.getValue();
                        enDiscussion.add(name);
                %>
                <span class="onglet_0 onglet" id="onglet_<%= name%>" onclick="javascript:change_onglet('<%= name%>');"><%= namAffiche%></span>
                <% }
                    newMessage.clear();
                %>

            </div>
            <div class="contenu_onglets" accept-charset="ISO-8859-1">
                <% for (Entry<String, String> entry : onglets.entrySet()) {
                        name = entry.getKey();
                        conv = entry.getValue();
                %>
                <div class="contenu_onglet" id="contenu_onglet_<%= name%>">
                    <%= conv%>
                </div>
                <% }%>

            </div>


            <div id="emission" name="Emission">
                <div id="zonesend" style="width:65%;">
                <form method="post" action="SendMessage" name="SendArea" accept-charset="ISO-8859-1">
                    <input type="hidden" name="dest" id="destinataire" value="">
                    <textarea id="messageToSendArea" name="messageToSendArea" ><%=messageToSend%></textarea>
                    <input type="hidden" name="scrollYInputSendMsg" value="" id="scrollYSendMsg">
                    <input type="submit" value="Send" hidden="true" id="sendMsgSubmit">
                    <input type="button" value="Send" onClick="sendMsg()" id="sendButton"/>
                    <a href='recordingpage.html'><img src="Pictures/microphone.png" style="margin-top:3px;height:20px; width:20px;"></a>
                    
                    <script type="text/javascript">
                        function sendMsg() {
                            document.getElementById('scrollYSendMsg').value = $(document).scrollTop();
                            document.getElementById('sendMsgSubmit').click();
                        }
                    </script>
                 </form>
                </div>
                    
                <div id="button1" style="width:30%; float:left; margin-left: 1%">
                    <form method="POST" action="FileUpload" enctype="multipart/form-data">
                        <input type="hidden" name="menu" value="false"/>
                        <input type="hidden" name="dest" id="destinatairefile" value="">
                        <input type="file" name="file" id="file" onclick="changeTime()"><br>
                        <input type="submit" value="Upload" name="upload" id="upload">
                        <%= filesent%>
                    </form>
                    <script type="text/javascript">
                        function changeTime() {
                            time = 35000; // on rajoute 35 secondes pour envoyer le fichier
                        }
                    </script>
                </div>

                <!--
                <div id="refresh">
                <input type="button" value="Refresh" onClick="refresh()" />
                </div>
                -->
            </div>


        </div>
        <script type="text/javascript">
            //<!--
            var anc_onglet = '<%= ongletactif%>';
            change_onglet(anc_onglet);
            //-->
        </script>

        <script type="text/javascript">
            //$(window).scrollTop($('#emission').offset().top);
            $(window).scrollTop(<%=ancientPageYScroll%>);
            //$('#messageToSendArea').focus();
            setCaretPosition(document.getElementById('messageToSendArea'),<%=ancientCaretPos%>);
        </script>

        <form method="POST" action="Refresh" name="refreshPost" accept-charset="ISO-8859-1">
            <input type="hidden" name="messageToSendInput" value="" id="refreshInput">
            <input type="hidden" name="scrollYInput" value="" id="scrollY">
            <input type="hidden" name="caretPos" value="" id="caretPos">
            <input type="submit" hidden="true" id="refreshSubmit">
        </form>



        <script type="text/javascript" >
            function refresher() {
                //rescuefieldvalues(['messageToSendArea']);
                //javascript:location.reload(true);
                //var msg = document.getElementById('messageToSendArea').value;
                //javascript:refresh(msg,document.getElementById('messageToSendArea'));
                document.getElementById('refreshInput').value = document.getElementById('messageToSendArea').value;
                document.getElementById('scrollY').value = $(document).scrollTop();
                document.getElementById('caretPos').value = doGetCaretPosition(document.getElementById('messageToSendArea'));
                document.getElementById('refreshSubmit').click();
            }
            function doWait() {
                setTimeout("refresher()", 1 + time);
            }
            setTimeout("doWait()", 10000);
        </script>
    </body>
</html>
